import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Maze {
    private static int[] x = {-1, 0, 0, 1};
    private static int[] y = {0, -1, 1, 0};
    private static boolean[][] vis;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int m = in.nextInt();
            in.nextLine();
            int xs = in.nextInt() - 1;
            int ys = in.nextInt() - 1;
            int xt = in.nextInt() - 1;
            int yt = in.nextInt() - 1;
            in.nextLine();
            char[][] board = new char[n][m];
            vis = new boolean[n][m];
            for (int i = 0; i < n; i++) {
                String str = in.nextLine();
                board[i] = str.toCharArray();
            }
            if (board[xt][yt] == '*') {
                System.out.println(-1);
                return;
            }
            int ret = bfs(board, xs, ys, xt, yt);
            System.out.println(ret);
        }
    }

    private static int bfs(char[][] board, int xs, int ys, int xt, int yt) {
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[] {xs, ys, 0});
        vis[xs][ys] = true;
        while (!queue.isEmpty()) {
            int[] tmp = queue.poll();
            if (tmp[0] == xt && tmp[1] == yt) {
                return tmp[2];
            }
            for (int i = 0; i < 4; i++) {
                int newRow = tmp[0] + x[i];
                int newCol = tmp[1] + y[i];
                if (newRow >= 0 && newRow < board.length
                        && newCol >= 0 && newCol < board[0].length
                        && !vis[newRow][newCol]
                        && board[newRow][newCol] == '.') {
                    vis[newRow][newCol] = true;
                    queue.offer(new int[] {newRow, newCol, tmp[2] + 1});
                }
            }
        }
        return -1;
    }
}
